#include "file.h"
#include "mutex.h"
#include "klib.h"

static file_t  file_table[FILE_TABLE_SIZE];
static mutex_t file_alloc_mutex;

file_t *file_alloc(void)
{
    mutex_lock(&file_alloc_mutex);
    file_t *free_file = (file_t *)0;
    for (int i = 0; i < FILE_TABLE_SIZE; i++)
    {
        file_t *p_file = &file_table[i];
        if (p_file->ref == 0)
        {
            free_file      = p_file;
            free_file->ref = 1;
            break;
        }
    }
    mutex_unlock(&file_alloc_mutex);
    return free_file;
}
void file_free(file_t *file)
{
    mutex_lock(&file_alloc_mutex);
    if (file->ref)
    {
        file->ref--;
    }
    mutex_unlock(&file_alloc_mutex);
}
void file_table_init(void)
{
    mutex_init(&file_alloc_mutex);
    kernel_memset(file_table, 0, sizeof(file_table));
}
void file_inc_ref(file_t *file)
{
    mutex_lock(&file_alloc_mutex);
    file->ref++;
    mutex_unlock(&file_alloc_mutex);
}